<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesMaterialStationInventoryTable"
      >
      </ele-page>
    </div>
    <ele-form-drawer class="dialog-pack" size="20%" :isLoading="isLoading" v-model="formDataDialog" v-bind="formDescDialog" :request-fn="handleSubmit" :visible.sync="visible" :title="title">
    </ele-form-drawer>
  </div>
</template>

<script>
import { exportExcel } from '@/utils/judge'
import { getBaseCodeStrategy } from '@/api/common'
import { getUnitDictList } from '@/api/common'
import url from '@/axios/url'
const { minioUrl } = url()
import { operateList, searchConfig, importFormDescDialog, formDescDialog, rowConfig } from './config'
import { getList, rowCreate, rowUpdate, rowDel, rowDetail, importData, getTypeCodeList, getStatusList } from '@/api/materialStation/mesMaterialStationInventory'

export default {
  name: 'mesMaterialStationInventory',
  mixins: [operateList],
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: { codeStrategy: '' },
      formDescDialog, //新增修改框
      searchConfig,
      getList,
      isLoadingImport: false, // 导入数据loading
      importData: {}, //导入数据
      importVisible: false,
      importFormDescDialog, // 导入字段配置
      // 列表页字段的绑定配置
      column: [
        {
          type: 'selection'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60,
          fixed: 'left'
        },
        {
          prop: 'stationCode',
          label: $i18n.t('mesMaterialStationInventory.stationCode'),
          minWidth: 150,
          showOverflowTooltip: true
        },
        {
          prop: 'stationName',
          label: $i18n.t('mesMaterialStationInventory.stationName'),
          minWidth: 250,
          showOverflowTooltip: true
        },
        {
          prop: 'volume',
          label: $i18n.t('mesMaterialStationInventory.volume'),
          minWidth: 100,
          showOverflowTooltip: true
        },
        {
          prop: 'unit',
          label: $i18n.t('mesMaterialStationInventory.unit'),
          minWidth: 100,
          showOverflowTooltip: true,
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            const list = await getUnitDictList()
            return list.data.result
          }
        },
        {
          prop: 'typeCode',
          label: $i18n.t('mesMaterialStationInventory.typeCode'),
          minWidth: 100,
          showOverflowTooltip: true,
          optionProp: {
            value: 'typeCode',
            label: 'typeName'
          },
          option: async (data) => {
            const list = await getTypeCodeList()
            return list.data.result
          }
        },
        {
          prop: 'status',
          label: $i18n.t('mesMaterialStationInventory.status'),
          minWidth: 100,
          showOverflowTooltip: true,
          optionProp: {
            value: 'value',
            label: 'description'
          },
          option: async (data) => {
            const list = await getStatusList()
            return list.data.result
          }
        },
        {
          prop: 'createByName',
          label: $i18n.t('common.createByName'),
          minWidth: 130,
          showOverflowTooltip: true
        },
        {
          prop: 'createTime',
          label: $i18n.t('common.createTime'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        {
          prop: 'updateByName',
          label: $i18n.t('common.updateByName'),
          minWidth: 130,
          showOverflowTooltip: true
        },
        {
          prop: 'updateTime',
          label: $i18n.t('common.updateTime'),
          minWidth: 180,
          showOverflowTooltip: true
        },
        {
          prop: 'operate',
          label: $i18n.t('common.operation'),
          width: 160,
          fixed: 'right',
          render: (h, scope) => {
            return <ele-row-operation scope={scope} rowConfig={rowConfig} onRowOperate={this.rowOperate} />
          }
        }
      ]
    }
  },
  created() {
    this.getBaseCodeStrategy()
  },
  computed: {},
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name
      const { codeStrategy } = this.formDataDialog
      const fnc = {
        add: () => {
          this.disableFields(false)
          this.formDataDialog = { status: '1', codeStrategy }
          this.visible = true
        },
        deleteBatch: () => {
          this.$confirm($i18n.t('common.deleteBatch', { name: $i18n.t('mesMaterialStationInventory.name') }), $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t('common.ok'),
            cancelButtonText: $i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            const ids = data.rows.map((val) => val.id).join(',')
            this.rowDel(ids)
          })
        },
        template: () => {
          // 系列管理模板
          var urlexportExcel = `${minioUrl}/mes-template/料站清单模板.xls`
          window.open(urlexportExcel)
        }
      }
      fnc[data.handleName]()
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name
      const fnc = {
        edit: () => {
          this.disableFields(false)
          this.rowDetail(row.id)
          this.visible = true
        },
        detail: () => {
          this.disableFields(true)
          this.rowDetail(row.id)
          this.visible = true
        },
        delete: () => {
          this.$confirm($i18n.t('common.deleteSingle', { name: $i18n.t('mesMaterialStationInventory.name') }), $i18n.t('common.tip'), {
            confirmButtonText: $i18n.t('common.ok'),
            cancelButtonText: $i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            this.rowDel(row.id)
          })
        }
      }
      fnc[handleName]()
    },
    doSubmit({ title, api, data }) {
      this.isLoading = true
      api(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || title)
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return // 判断状态
      delete data.factoryCode // 删除不需要修改的非主键字段
      delete data.remark // 删除不需要修改的非主键字段
      delete data.userDefined1 // 删除不需要修改的非主键字段
      delete data.userDefined2 // 删除不需要修改的非主键字段
      delete data.userDefined3 // 删除不需要修改的非主键字段
      delete data.userDefined4 // 删除不需要修改的非主键字段
      delete data.userDefined5 // 删除不需要修改的非主键字段
      delete data.userDefined6 // 删除不需要修改的非主键字段
      delete data.userDefined7 // 删除不需要修改的非主键字段
      delete data.userDefined8 // 删除不需要修改的非主键字段
      delete data.userDefined9 // 删除不需要修改的非主键字段
      delete data.userDefined10 // 删除不需要修改的非主键字段
      delete data.createBy // 删除不需要修改的非主键字段
      delete data.createByName // 删除不需要修改的非主键字段
      delete data.createTime // 删除不需要修改的非主键字段
      delete data.updateBy // 删除不需要修改的非主键字段
      delete data.updateByName // 删除不需要修改的非主键字段
      delete data.updateTime // 删除不需要修改的非主键字段
      delete data.activeFlag // 删除不需要修改的非主键字段
      const params = this.formDataDialog.id ? { title: $i18n.t('common.editSuccess'), api: rowUpdate, data } : { title: $i18n.t('common.addSuccess'), api: rowCreate, data }
      this.doSubmit(params)
    },
    //删除
    rowDel(data) {
      this.loading = true
      rowDel(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || $i18n.t('common.deleteSuccess'))
          this.refresh()
        })
        .finally(() => {
          this.loading = false
        })
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag
      this.formDescDialog.isShowSubmitBtn = !flag
    },
    rowDetail(id) {
      rowDetail(id).then((res) => {
        this.formDataDialog = res.data.result
      })
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset)
    },
    //数据文件上传
    upLoadHandleChange(file) {
      this.$set(this.importData, 'file', file)
    },
    // 提交导入信息
    handleSubmitImport() {
      if (!this.importData.file) {
        this.$message.error($i18n.t('common.uploadMsg'))
        return
      }
      const formData = new FormData()
      formData.append('file', this.importData.file.get('file'))
      this.isLoadingImport = true
      importData(formData)
        .then((res) => {
          this.$message.success($i18n.t('common.importSuccess'))
          this.isLoadingImport = false
          this.importVisible = false
          this.refresh()
        })
        .catch((error) => {
          console.log('错误', error)
          this.$set(this.importData, 'file', null)
        })
        .finally(() => {
          this.isLoadingImport = false
          this.$refs.uploadNoAuto.clearFiles()
        })
    },
    //判断是否存在编码规则
    getBaseCodeStrategy() {
      getBaseCodeStrategy({
        code: 'materialStaionList',
        system: 'default',
        strategyStatus: '1'
      }).then((res) => {
        // console.log("判断是否存在编码规则", res);
        const result = res.data.result
        this.formDataDialog.codeStrategy = Array.isArray(result) && result.length > 0 ? result[0].code : ''
      })
    }
  }
}
</script>
